package com.voice.server.publish.device;

import com.alibaba.fastjson.JSONObject;
import com.voice.core.connection.entity.Stream;
import com.voice.core.connection.entity.StreamData;
import com.voice.server.connection.ConnectionInvoke;
import com.voice.server.connection.DeviceControllerConnection;
import com.voice.server.message.sdk.PenetrateMessage;
import com.voice.server.publish.PublishHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("/device/message/penetrate")
public class PenetrateHandler implements PublishHandler<DeviceControllerConnection> {

    @Autowired
    private ConnectionInvoke connectionInvoke;

    @Override
    public void handle(Stream stream,StreamData streamData, DeviceControllerConnection connection){
        JSONObject jsonObject=JSONObject.parseObject(new String(streamData.readAllData()));
        if(jsonObject==null) return;
        byte[] data=jsonObject.getBytes("data");
        PenetrateMessage penetrateMessage=new PenetrateMessage(connection.getUserId(),connection.getDeviceKey(),data);
        this.connectionInvoke.sendSdkMessage(connection.getUserId(), penetrateMessage.getTopic(),penetrateMessage);
    }
}
